<%@ include file="../import_page.jsp"%>
<%@ page import="org.json.*"%>
<%
	//login checking
	if(!loginedChecking(session, UsersType.staff)) {
		//user don't login
		//redirect to home page
		response.sendRedirect(URL_HOMEPAGE);
		return;
	}
	
	//select unit check
	if(!selectUnitCheck(session)) {
		//if user didn't select an Unit
		//redirtect to select page
		response.sendRedirect(URL_SELECT_UNIT);
		return;
	}
	
	Units selectUnit = (Units)session.getAttribute(SESSION_SELECT_UNIT);
	String errMsg = null;
	AssignmentCollector ac = null;
	
	String acIdStr = request.getParameter(PAR_ASSIGNMENT_ID);
	//try to catch Assignment ID input
	AssignmentsControl aCon = new AssignmentsControl();
	try {
		int acId = Integer.parseInt(acIdStr);
		ac = aCon.getAssignment(acId);
		//Check the unit of assignment is match the selected unit
		if(ac.getUnitID().indexOf(selectUnit.getId()) < 0)
			throw new Exception("Select Unit do not match the Assignment collector Unit");
		
	} catch (Exception err) {
		aCon.closeDBConnection();
		response.sendRedirect(URL_VIEW_ASSIGNMENT);
		return;
	}
	
	//catch marking scheme of assignment collector
	String jArrText = "null";
	try {
		//get Assignment Material
		ArrayList<AssignmentMaterial> listAm = aCon.getAssignmentMaterial(ac.getId());
		aCon.closeDBConnection();
		//convert to json string
		jArrText = (new JSONArray(listAm)).toString();
	} catch (Exception err) {
		aCon.closeDBConnection();
		errMsg = err.toString();
	}
%>
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>Modify Assignment Material</title>
<script type="text/javascript" src="<%= URL_JQUERY %>"></script>
<script type="text/javascript">
//input marking scheme
var arrayAm = <%= jArrText%>;
var acId = <%=ac.getId() %>;
</script>
<script type="text/javascript" src="../js/f_modify_material.js"></script>
<link rel="stylesheet" type="text/css" href="../img_style.css" />
</head>
<body>
	<table border="1">
		<tr>
			<td>Unit (ID)</td>
			<td><%= selectUnit.getName() %> (<%=selectUnit.getId() %>)</td>
		</tr>
		<tr>
			<td>Assignment Name (ID)</td>
			<td><%= ac.getName() %> (<%=ac.getId() %>)</td>
		</tr>
	</table>
	<hr />
	<form id="am_form"
		action="<%= addParmeterToUrl(URL_CONFIRM_AM, PAR_ASSIGNMENT_ID, Integer.toString(ac.getId())) %>"
		method="post" enctype="multipart/form-data">
		<table id="am_table" border="1">
			<tr>
				<th>Material</th>
				<th>Function</th>
			</tr>
			<!-- arrayAm information in here -->
		</table>
		<input type="image" src="../images/button/submit.gif" alt="submit"
			style="width: 102.5px; height: 40px" />
	</form>
	<%if(!isEmptyString(errMsg)) {%>
	<p><%=errMsg %></p>
	<%} %>
</body>
</html>